x = 'Pluto is a planet'
y = "Pluto is a planet"
print(x == y)

print("Pluto's a planet!")
print('My dog is named "Pluto"')
print("My dog is named 'Pluto' and 'dddd'")

print('"Pluto\'s a planet!')

hello = "hello\nworld"
print(hello)

triplequoted_hello = """hello
world"""
print(triplequoted_hello)
print(triplequoted_hello == hello)

print('============Strings are sequences=============')
# Indexing
planet = 'Pluto'
print(planet[3])
print(len(planet))
print(planet[-3:])

char_seq = [char + '#' for char in planet]
print(char_seq)
# But a major way in which they differ from lists is that they are immutable. We can't modify them.
# planet[0] = 'B'


print("======String methods======")
# ALL CAPS
claim = "Pluto is a planet!"
print(claim.upper())
# Searching for the first index of a substring
print(claim.lower())
print(claim.index('is'))
# claim.startswith(planet)
# True
# # false because of missing exclamation mark
# claim.endswith('planet')
# False

"""
Going between strings and lists: .split() and .join()
str.split() turns a string into a list of smaller strings, breaking on whitespace by default.
str.join() takes us in the other direction, sewing a list of strings up into one long string, using the string it was called on as a separator. 
str.format() on a "format string", where the Python values we want to insert are represented with {} placeholders
"""
print("=======.split()  .join() .format()===========")
print(claim)
print(claim.split())

datastr = '1993-01-28'
spilt_str = datastr.split('-')
print(spilt_str)
# spilt_str.append('00:00:00')
# year, month, day = spilt_str
#     ^^^^^^^^^^^^^^^^
# ValueError: too many values to unpack (expected 3)
year, month, day = spilt_str
print(year, month, day)

print('/'.join([month, day, year]))
print(' 👏 '.join([word.upper() for word in planet]))  # 特殊字符也是str类，可以调用join()

position = 9
# planet + ", you'll always be the " + position + "th planet to me."
print(planet + ", you'll always be the " + str(position) + "th planet to me.")
# help(str)

# We call .format() on a "format string", where the Python values we want to insert are represented with {} placeholders
print("{}, you'll always be the {}th planet to me.".format(planet, position))

pluto_mass = 1.303 * 10 ** 22
earth_mass = 5.9722 * 10 ** 24
population = 52910390
#         2 decimal points   3 decimal points, format as percent     separate with commas
print("{} weighs about {:.2} kilograms ({:.3%} of Earth's mass). It is home to {:,} Plutonians.".format(
    planet, pluto_mass, pluto_mass / earth_mass, population,
))

# Referring to format() arguments by index, starting from 0
s = """Pluto's a {0}.
No, it's a {2}.
{0}!
{1}!""".format('planet', ';test', 'dwarf planet')
print(s)

"""
Dictionaries
Dictionaries are a built-in Python data structure for mapping keys to values.
Python has dictionary comprehensions with a syntax similar to the list comprehensions we saw in the previous tutorial.
"""
print("========Dictionaries==========")
# []list ()tuple {}dictionary
numbers = {'one': 1, 'two': 2, 'three': 3}
print(numbers["one"])
# print(numbers["1"])
#     print(numbers["1"])
#           ~~~~~~~^^^^^
# KeyError: '1'
numbers['eleven'] = 11
print(numbers)
numbers['one'] = 'Pluto'
print(numbers)

print("======== dictionary comprehensions ==========")
planets = ['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune']
print(type(planets))
planet_to_initial = {planet: planet[0] for planet in planets}
print(planet_to_initial)
print(type(planet_to_initial))

print('Saturn' in planet_to_initial)
print('Betelgeuse' in planet_to_initial)
#遍历dictionary中的key
for k in numbers:
    print("{} = {}".format(k, numbers[k]))

# We can access a collection of all the keys or all the values with dict.keys() and dict.values(), respectively.
# Get all the initials, sort them alphabetically, and put them in a space-separated string.
print(' '.join(sorted(planet_to_initial.values())))

# dict.items()
for planet, initial in planet_to_initial.items():
    print("{} begins with \"{}\"".format(planet.rjust(10), initial))

help(str)